<?php
  require_once "function.php";

  // 沒有輸入姓名者，更新其基本資料 (name & age)

  $dbh=get_dbh();
  $sql="select 姓名,病歷號,影像日期,天 from pool where 天 is NULL or 姓名 is NULL or 歲 is NULL or 月 is NULL";
  $counts=0;
  foreach($dbh->query($sql) as $row){
    if($row['天']==NULL||$row['姓名']==NULL){
      $dbh2=get_dbh();
      $id=$row['病歷號'];
      $name=getName($id);
      if($name['gender']=='M') $gender='男';
      if($name['gender']=='F') $gender='女';
      $age=ageOI($name['birthdate'],str_replace('-','.',$row['影像日期']));
      $sql2="update pool set 姓名='".$name['name']."',歲='".$age['y']."',月='".$age['m']."',天='".$age['d']."',性別='".$gender."' where 病歷號='".$id."'";
      $count=$dbh2->exec($sql2);
      $counts=$counts+$count;
    }
  }
  echo "共更新".$counts."筆基本資料";

?>
